package com.fone.player.activity.personal;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.Rst;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;

public class FeedBackActivity extends BaseActivity implements OnClickListener {
	
	private static final String TAG = "FeedBackActivity";
	
	private TextView titleTextView;
	private Button leftBt, rightBt,feedback_submit;
	private EditText contentText,contactText;
	private String content,contact;
	private String contact_type;
	private LinearLayout waitting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feed_back_view);
		initView();
	}

	private void initView() {
		leftBt = (Button) findViewById(R.id.common_title_left_bt);
		rightBt = (Button) findViewById(R.id.common_title_right_bt);
		feedback_submit = (Button) findViewById(R.id.feedback_submit);
		titleTextView = (TextView) findViewById(R.id.common_title_text_tv);
		contentText = (EditText) findViewById(R.id.feedback_content_edit);
		contactText = (EditText) findViewById(R.id.feedback_contact_edit);
		waitting = (LinearLayout) findViewById(R.id.feedback_waitting_ll);

		leftBt.setText("用户反馈");

		rightBt.setVisibility(View.GONE);
		titleTextView.setVisibility(View.GONE);
		leftBt.setOnClickListener(this);
		feedback_submit.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_title_left_bt:
			hideSoftKeyboard(feedback_submit);
			finish();
			break;
		case R.id.feedback_submit:
			if(validateFeedback() && FoneUtil.isNetOkWithToast(mContext)){
				// 隐藏输软键盘
				hideSoftKeyboard(feedback_submit);
				waitting.setVisibility(View.VISIBLE);
				Request.getInstance().fb(content, contact, contact_type, new FbCallBack());
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 隐藏软键盘
	 * @param view
	 */
	private void hideSoftKeyboard(View view){
		InputMethodManager imm = (InputMethodManager) mContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null) {
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
	
	private boolean validateFeedback() {
		content = contentText.getText().toString().trim();
		contact = contactText.getText().toString().trim();
		if (content == null || content.length() == 0) {
			FoneUtil.showToast(mContext, R.string.feedback_err);
			return false;
		}else if (content.length()>500) {
			FoneUtil.showToast(mContext, "输入内容超过500字,请重新编辑");
			return false;
		}
		
		if (contact != null && contact.length() > 0) {
			contact_type = getContactType(contact);
			L.v(TAG, "contact_type : ", contact_type);
			if (TextUtils.isEmpty(contact_type)) {
				FoneUtil.showToast(mContext, R.string.feedback_contact_err);
				return false;
			}
		} else {
			FoneUtil.showToast(mContext, R.string.feedback_contact_err);
			return false;
		}

		return true;
	}
	
	private String getContactType(String contact) {
		if (contact.length() < 12) {
			if (isPhone(contact)) {
				//Toast.makeText(mContext, "验证为手机号", Toast.LENGTH_SHORT).show();
				return "phone";
			} else if (isEmail(contact)) {
				//Toast.makeText(mContext, "验证为电子邮件", Toast.LENGTH_SHORT).show();
				return "email";
			} else if (isQQ(contact)) {
				//Toast.makeText(mContext, "验证为QQ号", Toast.LENGTH_SHORT).show();
				return "qq";
			}
		} else {
			if (contact.indexOf("@") > 0) {
				//超过13个字符 正则会出ANR
				//Toast.makeText(mContext, "验证为电子邮件较长", Toast.LENGTH_SHORT).show();
				return "email";
			}
		}
		return null;
	}

	/**
	 * 判断是否为电话号
	 * @param contact
	 * @return
	 */
	private boolean isPhone(String contact) {
		// 电话号码
		String checkPhone = "^(13[0,1,2,3,4,5,6,7,8,9]|15[0,1,2,3,5,6,7,8,9]|18[0,1,2,3,5,6,7,8,9])\\d{8}$";
		
		Pattern regex = Pattern.compile(checkPhone);
		Matcher matcher = regex.matcher(contact);
		boolean isMatched = matcher.matches();
		
		return isMatched;
	}

	private boolean isEmail(String contact) {
		// 电子邮件
		String checkEmail = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern regex = Pattern.compile(checkEmail);
		Matcher matcher = regex.matcher(contact);
		boolean isMatched = matcher.matches();
		
		return isMatched;
	}

	private boolean isQQ(String contact) {
		// QQ
		//String checkEmail = "[1-9]\\d{4,9}";
		String checkEmail = "([1-9][0-9]{4})|([0-9]{6,10})";
		Pattern regex = Pattern.compile(checkEmail);
		Matcher matcher = regex.matcher(contact);
		boolean isMatched = matcher.matches();
		
		return isMatched;
	}
	
	private class FbCallBack implements Callback<Rst> {

		@Override
		public void onSuccess(Rst t) {
			L.v(TAG, "FbCallBack onSuccess", t.toString());
			if(t.result!=0){
				FoneUtil.showToast(mContext, "信息反馈失败，请重试");
				waitting.setVisibility(View.GONE);
				return;
			}
			FoneUtil.showToast(mContext, "信息反馈成功");
			waitting.setVisibility(View.GONE);
			finish();
		}

		@Override
		public void onFailure(Error error) {
			waitting.setVisibility(View.GONE);
			L.v(TAG,
					"FbCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());
			
			if(error.isNetworkError()){
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return ;
			}
			FoneUtil.showToast(mContext, "信息反馈失败，请重试");
		}
		
	}
	
}
